﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Ghosthunt/Cheese.cs
//  *
//  ***************************************************************************

#endregion

using Ghosthunt.Resources;

using Haplous;

namespace Ghosthunt
{
    /// <summary>
    ///     A version of the <see cref="Scenery" /> class that can be used to represent the strawberries that Ghosthunter eats.
    /// </summary>
    public class Cheese : Scenery
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="Wall" /> class.
        /// </summary>
        /// <param name="game">
        ///     The game to which the class belongs.  The <paramref name="game" /> must not be
        ///     <see langword="null" />.
        /// </param>
        /// <param name="name">
        ///     The unique name that can be used to identify this object.  The <paramref name="name" /> must not be
        ///     <see langword="null" />, empty or all whitespace.
        /// </param>
        public Cheese(Game game, string name) : base(game, name)
        {
            Game.ActiveMode = Game.Modes["Normal"];

            if(!game.Pictures.Contains("Cheese"))
                game.AddPicture("Cheese", ResourcesHelper.GetBitmap("Cheese"));

            PictureSource.Source = game.Pictures["Cheese"];
            Visualization.Visible = true;
            Visualization.Origin = OriginKind.Center;
            Visualization.Scaling = PictureScalingKind.Stretch;
            Visualization.Tiling = TileKind.None;

            Size.Set(32, 32);

            // Now make sure that the appearance and behaviour when in "Ghost Hunting" mode matches normal mode
            CopyModeProperties(game.Modes["Normal"], game.Modes["GhostHunt"], new[] {"Director"});
        }

        /// <summary>
        ///     Called when the active mode has changed.
        /// </summary>
        /// <param name="previousMode">The previous mode.</param>
        /// <param name="newMode">The new mode.</param>
        protected override void OnModeChanged(Mode previousMode, Mode newMode)
        {
            if(previousMode == null)
                return;

            CopyModeProperties(previousMode, newMode, new[] {"Opacity"});
        }

        /// <summary>
        ///     Occurs when an item that is attempting to move is found to be in collision with this item.
        /// </summary>
        /// <param name="item">The item that is attempting to move.</param>
        /// <param name="oldPosition">The starting position of the item that has changed.</param>
        /// <param name="oldSize">The original size of the item that has changed.</param>
        /// <param name="newPosition">The proposed new position of the item that has changed.</param>
        /// <param name="newSize">The proposed new size of the item that has changed.</param>
        /// <returns>The outcome of the collision.</returns>
        /// <remarks>
        ///     The intention of this method is to allow the receiver to make a decision about, but not yet react, to an impact.
        ///     The reaction should occur when the <see cref="SolidGraphicBlock.OnCollision" /> method is called.
        /// </remarks>
        public override OutcomeKind OnColliding(SolidGraphicBlock item, PositionInt32 oldPosition, SizeInt32 oldSize, PositionInt32 newPosition, SizeInt32 newSize)
        {
            // If it is Ghosthunter, be "eaten"
            if((item is Ghosthunter) && Visualization.Visible)
                return OutcomeKind.Reactive;

            return OutcomeKind.Permitted;
        }

        /// <summary>
        ///     Called when another element has moved and a collision has occurred.
        /// </summary>
        /// <param name="item">The item that has moved.</param>
        /// <param name="anticipatedOutcome">
        ///     The anticipated outcome from the previous call to
        ///     <see cref="SolidGraphicBlock.OnColliding" />.
        /// </param>
        /// <remarks>This method is called to allow the recipient to react to the collision.</remarks>
        public override void OnCollision(SolidGraphicBlock item, OutcomeKind anticipatedOutcome)
        {
            if(!Visualization.Visible)
                return;

            if(!(item is Ghosthunter))
                return;

            Visualization.Visible = false;
            ((Ghosthunt)Game).CheeseEaten();
        }
    }
}